package cn.fyl.system.controller;

import cn.fyl.basic.util.JsonResult;

import cn.fyl.basic.util.PageList;
import cn.fyl.system.domain.SystemDetail;
import cn.fyl.system.domain.SystemType;
import cn.fyl.system.query.SystemTypeQuery;
import cn.fyl.system.service.ISystemDetailService;
import cn.fyl.system.service.ISystemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class SystemTypeController {

    @Autowired
    private ISystemTypeService systemTypeService;

    @Autowired
    private ISystemDetailService systemDetailService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<SystemType> findAll(){
        return systemTypeService.loadAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public SystemType findById(@PathVariable("id") Long id){
        return systemTypeService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            List<SystemDetail> list = systemDetailService.loadAll();
            for (SystemDetail systemDetail : list) {
                if(systemDetail.getTypes_id()==id){
                    Long systemDetailId = systemDetail.getId();
                    systemDetailService.remove(systemDetailId);
                }
            }
            systemTypeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 添加和修改操作
     * @param SystemType
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody SystemType SystemType){
        try {
            if(SystemType.getId()==null){
                systemTypeService.save(SystemType);
            }else {
                systemTypeService.update(SystemType);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PutMapping
    public PageList<SystemType> queryPage(@RequestBody SystemTypeQuery query){
        try {
            return systemTypeService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SystemType>();
        }
    }

    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            List<SystemDetail> list = systemDetailService.loadAll();
            for (Long id : ids) {
                for (SystemDetail systemDetail : list) {
                    if(systemDetail.getTypes_id()==id){
                        Long systemDetailId = systemDetail.getId();
                        systemDetailService.remove(systemDetailId);
                    }
                }
            }

            systemTypeService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


}
